<?php

namespace app\api\controller\wanlshop;

use addons\wanlshop\library\EasyWeChat\Easywechat;
use app\common\controller\Api;
use fast\Tree;
use think\Request;
use think\Response;

/**
 * WanlShop店铺接口
 */
class Shop extends Api
{
    protected $noNeedLogin = ['getShopInfo','qrcode'];
    protected $noNeedRight = ['*'];
	
	public function _initialize()
	{
	    parent::_initialize();
		$this->model = model('app\api\model\wanlshop\Shop');
	}

	//店铺列表
    public function shoplist()
    {
        $list = $this->model->paginate(10, false, ['query' => request()->get()]);
        $this->success('返回成功', $list);
    }

    public function shopinfo()
    {
        $id = input('id',0);
        $row = $this->model->get($id);
        if (!$row) {
            $this->error(__('未找到此商家'));
        }
        $this->success('返回成功', $row);
    }
	
	/**
	 * 一次性获取店铺相关数据 1.0.8升级
	 *
	 * @ApiSummary  (WanlShop 一次性获取店铺相关数据)
	 * @ApiMethod   (GET)
	 *
	 * @param string $id 页面ID
	 */
	public function getShopInfo($id = null)
	{
		//设置过滤方法
		$this->request->filter(['strip_tags']);
		// 获取店铺信息
		$row = $this->model->get($id);
		if (!$row) {
		    $this->error(__('未找到此商家'));
		}
		// 获取商家类目
		$tree = Tree::instance();
		$tree->init(
			model('app\api\model\wanlshop\ShopSort')
				->where(['shop_id' => $row['id']])
				->field('id, pid, name, image')
				->order('weigh asc')
				->select()
		);
		$row['category'] = $tree->getTreeArray(0);
		// 查看是否被关注
		$row['isFollow'] = model('app\api\model\wanlshop\find\Follow')
			->where([
				'user_no' => $row['find_user']['user_no'], 
				'user_id' => $this->auth->id
			])
			->count();
		$row['isLive'] = model('app\api\model\wanlshop\Live')
			->where(['shop_id' => $row['id'], 'state' => 1])
			->field('id')
			->find();
		// 获取类目样式配置
		$shopConfig = model('app\api\model\wanlshop\ShopConfig')
			->where(['shop_id' => $row['id']])
			->find();
		$row['categoryStyle'] = (int)$shopConfig['category_style'];
		// 获取商家自定义页面
		$row['page'] = model('app\api\model\wanlshop\Page')
			->where([
				'shop_id' => $row['id'], 
				'type' => 'shop'
			])
			->field('id, name, page, item')
			->find();
		$this->success('返回成功', $row);
	}

	//店铺收款二维码
	public function qrcode()
    {
        $shop_id = input('shop_id',0);
        $config = get_addon_config('qrcode');
        $params = $this->request->get();
        $params = array_intersect_key($params, array_flip(['text', 'size', 'padding', 'errorlevel', 'foreground', 'background', 'logo', 'logosize', 'logopath', 'label', 'labelmargin', 'labelfontsize', 'labelalignment', 'labelfontcolor']));

        //$params['text'] = $this->request->get('text', $config['text'], 'trim');
        //$params['label'] = $this->request->get('label', $config['label'], 'trim');
        $request = Request::instance();
        $domain=$request->domain();

        $params['text'] = $domain.'?shop_id='.$shop_id;
        $qrCode = \addons\qrcode\library\Service::qrcode($params);

        $qrcodePath = ROOT_PATH . 'public/uploads/shopqrcode/';
        if (!is_dir($qrcodePath)) {
            @mkdir($qrcodePath);
        }
        if (is_really_writable($qrcodePath)) {
            //$filePath = $qrcodePath . md5(implode('', $params)) . '.' . $config['format'];
            $filePath = $qrcodePath  . 'shop_id_'.$shop_id . '.' . $config['format'];
            $qrCode->saveToFile($filePath);
        }
        $path =  str_replace(ROOT_PATH.'public', "", $filePath);

        $this->success('',$path);
    }

}